<template>
  <el-dialog :title="!dataForm.expertId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" labelWidth="120px" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="专家姓名" prop="expertName">
        <el-input v-model="dataForm.expertName"></el-input>
      </el-form-item>
      <el-form-item label="专家性别" prop="expertSex">
        <el-radio-group v-model="dataForm.expertSex">
          <el-radio label="M">男</el-radio>
          <el-radio label="W">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="专家生日" prop="expertBirthday">
        <el-date-picker
            v-model="dataForm.expertBirthday"
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            placeholder="选择专家生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="专家证类型" prop="expertIndentifyType">
        <el-select v-model="dataForm.expertIndentifyType" placeholder="请选择专家证类型">
          <el-option :label="year.label" :value="year.value" v-for="(year,idx) of typeList" :key="idx"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工作单位" prop="expertAddress">
        <el-input v-model="dataForm.expertAddress"></el-input>
      </el-form-item>
      <el-form-item label="专家证号" prop="expertIndentifyNo">
        <el-input v-model="dataForm.expertIndentifyNo"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobileNo">
        <el-input v-model="dataForm.mobileNo"></el-input>
      </el-form-item>
      <el-form-item label="微信号" prop="wxId">
        <el-input v-model="dataForm.wxId"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
  </el-dialog>
</template>

<script>
import {
  Debounce
} from '@/utils/debounce'
export default {
  data() {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        expertName: "",
        expertSex: "M",
        expertBirthday: "",
        expertAddress: "",
        expertIndentifyType: "01",
        expertIndentifyNo: "",
        mobileNo: "",
        wxId: "",
        userId: this.$store.state.user.name,
        expertId: null
      },
      typeList: [
        { label : "猫科", value : "01" },
        { label : "狗科", value : "02" }
      ],
      dataRule: {
        expertName: [
          { required: true, message: '请输入专家姓名', trigger: 'blur' },
        ],
        expertIndentifyNo: [
          { required: true, message: '请输入专家证号', trigger: 'blur' },
        ],
        expertBirthday: [
          { required: true, message: '请选择专家生日', trigger: 'change' }
        ],
        expertAddress: [
          { required: true, message: '请输入专家证地址', trigger: 'blur' }
        ],
        estimateYear: [
          { required: true, message: '请选择评估年度', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(id,data) {
      this.dataForm.expertId = id || null
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.expertId) {
          this.dataForm = data
          // this.$http({
          //   url: this.$http.adornUrl('/admin/basExpert/' + this.dataForm.expertId),
          //   method: 'get',
          //   params: this.$http.adornParams()
          // }).then(({
          //   data
          // }) => {
          //   this.dataForm = data
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit: Debounce(function() {
      let addFlag = wfy.empty(this.dataForm.expertId);
      this.dataForm.expertId = wfy.empty(this.dataForm.expertId) ? wfy.uuid(20) : this.dataForm.expertId
      console.log('🚀 ~ this.dataForm', this.dataForm)
      console.log('🚀 ~ addFlag', addFlag)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/admin/basExpert'),
            method: addFlag ? 'post' : 'put',
            data: this.$http.adornData(this.dataForm)
          }).then(({
            data
          }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    })
  }
}
</script>
